package com.hzdl.car.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.User;
import com.hzdl.car.service.LogService;
import com.hzdl.car.service.UserService;
import com.hzdl.car.util.HzdlResultJson;
import com.hzdl.car.util.ResultObj;
import com.hzdl.car.vo.LogVo;
import com.hzdl.car.vo.UserVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;

@Controller
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private LogService logService;
    @RequestMapping("toLogin")
    public String toLogin(){
        return "system/main/login";
    }

    @RequestMapping("login")
    public String login(UserVo userVo , Model model, HttpSession session, HttpServletRequest request){
        User u = userService.login(userVo);
        if(u == null){
            model.addAttribute("loginError","您输入的账号或密码错误");
            return "system/main/login";

        }else{
            try {
                LogVo logVo=new LogVo();
                String name=u.getRealName()+"_"+u.getLoginName();
                logVo.setLoginName(name);
//                String ip= InetAddress.getLocalHost().getHostAddress();
                String ip=request.getRemoteAddr();
                logVo.setLoginIp(ip);
                logService.addLog(logVo);
            } catch (Exception e) {
                e.printStackTrace();
            }
            session.setAttribute("user",u);
            return "system/main/index";
        }
    }
    @ResponseBody
    @RequestMapping("findUser")
    public HzdlResultJson findUser(UserVo userVo){
        PageInfo<User> pageInfo=userService.findUser(userVo);
        return HzdlResultJson.ok(pageInfo.getTotal(),pageInfo.getList());
    }
    @ResponseBody
    @RequestMapping("addUser")
    public ResultObj addUser(UserVo userVo){
        try {
            userService.addUser(userVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }
    @ResponseBody
    @RequestMapping("updateUser")
    public ResultObj updateUser(UserVo userVo){
        try {
            userService.updateUser(userVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            return ResultObj.UPDATE_ERROR;
        }
    }
    @ResponseBody
    @RequestMapping("chkRoleUser")
    public HzdlResultJson chkRoleUser(UserVo userVo){
        try {
            int row=userService.chkRoleUser(userVo);
            return HzdlResultJson.error(row,"");
        }catch (Exception e){
            return HzdlResultJson.error(-1,"");
        }
    }
    @ResponseBody
    @RequestMapping("delUser")
    public ResultObj delUser(UserVo userVo){
        try {
            userService.delUser(userVo);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }
    @ResponseBody
    @RequestMapping("resetPwd")
    public ResultObj resetPwd(UserVo userVo){
        try {
            userService.resetPwd(userVo);
            return ResultObj.RESET_SUCCESS;
        }catch (Exception e){
            return ResultObj.RESET_ERROR;
        }
    }
    @ResponseBody
    @RequestMapping("userDelChk")
    public ResultObj userDelChk(@RequestParam(value = "userIds[]") Integer[] userIds){
        try {
           userService.userDelChk(userIds);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }
    @ResponseBody
    @RequestMapping("findRoleByUser")
    public HzdlResultJson findRoleByUser(UserVo userVo){
        try {
            return HzdlResultJson.ok(userService.findRoleByUser(userVo));
        }catch (Exception e){
            return HzdlResultJson.error(-1,e.getMessage());
        }
    }
    @ResponseBody
    @RequestMapping("saveRoleUser")
    public ResultObj saveRoleUser(UserVo userVo){
        try {
            userService.saveRoleUser(userVo);
            return ResultObj.DISPATCH_SUCCESS;
        }catch (Exception e){
            return ResultObj.DISPATCH_ERROR;
        }
    }

}
